<?
session_start();

include('./../config/db_settings.php');
include('./../pms/pms_inc.php');
include('bookmarks_inc.php');

$user = $_SESSION[$settings['session_prefix'].'user_id'];

//Are they logged in or not?
if(!$user)
{
}
else
{
	$post_id = $_REQUEST['post_id'];
	$note = $_REQUEST['text'];
	
	//echo $post_id;
	//echo "<br>";
	//echo $note;
	//echo "<br>";
	
	if(empty($post_id) && empty($note)){
		echo "Post # and note can not be both blank.";
		exit;
	}
	
	if(!empty($post_id))
	{
		if(is_int($post_id)){
			echo "Post # has to be valid or blank.";
			exit;
		}
		$post_id = intval($post_id);
	}else{
		$post_id = 0;
	}

	//Get your bookmark count
	//Find the percentage that your inbox is full
	$sql = mysql_query ("SELECT count(*) bm_count FROM ".$db_settings['bookmarks_table']." WHERE user_id='$user'");
	$row = mysql_fetch_array ($sql);
	$bm_count = $row['bm_count'];
	if($bm_count>=$bookmark_limit){
		echo "Sorry, your bookmark centre is full.";
		exit;
	}

	if($post_id>0){
		$sql = mysql_query ("SELECT count(*) bm_count FROM ".$db_settings['bookmarks_table']." WHERE user_id='$user' and post_id='$post_id'");
		$row = mysql_fetch_array ($sql);
		$bm_count = $row['bm_count'];

		if($bm_count>0){
			echo "Sorry, post #".$post_id." is already in your bookmark centre. You can't bookmark a post twice.";
			exit;
		}
	}

	$x_note = "";
	if(strlen($note)>0){
		$x_note=base64_encode($note);
	}
	
	//echo "inserting...<br>";
	$sqlstr = "INSERT INTO ".$db_settings['bookmarks_table']." (user_id,post_id,note) VALUES($user, $post_id, '$x_note')";
	//echo $sqlstr."<br>";
	$sql = mysql_query($sqlstr) or die (mysql_error());
	
	//echo $sql."<br>";
	//echo "done.<br>";
	
	header("Location:bookmarks.php");
}
?>